//
//  TradeCNYView.m
//  BTCoin
//
//  Created by Robin on 29/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "TradeCNYView.h"
#import "SliderMenuView.h"
#import "TradeBuyView.h"
#import "TradeSellView.h"
#import "TradeProxyView.h"
#import "TradeRecordView.h"
#import <LCActionSheet/LCActionSheet.h>
#import "TradeAlertView.h"
#define KEY_WINDOW  [UIApplication sharedApplication].keyWindow
@interface TradeCNYView ()<TradeBuyViewDelegate,TradeAlertViewDelegate,TradeSellViewDelegate>
@property (nonatomic, strong) SliderMenuView *menuView;
@property (nonatomic, strong) TradeBuyView *buyView;
@property (nonatomic, strong) TradeSellView *sellView;
@property (nonatomic, strong) TradeProxyView *proxyView;
@property (nonatomic, strong) TradeRecordView *recordView;
@property (nonatomic, strong) NSArray   *buyCoinArr;
@property (nonatomic, strong) NSArray   *sellCoinArr;

@end

@implementation TradeCNYView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initMenuView];
        [self initSlideView];
    }
    
    return self;
}

- (void)initMenuView {
    _buyCoinArr = @[@{@"titleName":@"CNY(USDT)",
                      @"coinType":@""},
                    @{@"titleName":@"ETH(USDT)",
                      @"coinType":@""},
                    @{@"titleName":@"ETC(USDT)",
                      @"coinType":@""},
                    @{@"titleName":@"LICT(USDT)",
                      @"coinType":@""},
                    @{@"titleName":@"LIVE(USDT)",
                      @"coinType":@""},
                    @{@"titleName":@"TUTT(USDT)",
                      @"coinType":@""},
                    
                    ];
    CGRect frame = Rect(0, 0, RectWidth(self.frame), RectHeight(self.frame));
    self.buyView = [[TradeBuyView alloc] initWithFrame:frame];
    self.buyView.backgroundColor = UIColorFromRGB(0xF9F9F9);
    self.buyView.delegate = self;
    
    self.sellView = [[TradeSellView alloc] initWithFrame:frame];
    self.sellView.backgroundColor = UIColorFromRGB(0xF9F9F9);
    self.sellView.backgroundColor = UIColorFromRGB(0xF9F9F9);
    self.sellView.delegate = self;
    
    self.proxyView = [[TradeProxyView alloc] initWithFrame:frame];
    self.proxyView.backgroundColor = UIColorFromRGB(0xF9F9F9);
    
    self.recordView = [[TradeRecordView alloc] initWithFrame:frame];
    self.recordView.backgroundColor = UIColorFromRGB(0xF9F9F9);
}

- (void)initSlideView {
    
    @WeakObj(self)
    CGRect rect = CGRectMake(0,0, RectWidth(self.frame),RectHeight(self.frame));
    _menuView = [[SliderMenuView alloc] initWithFrame:rect withTitles:@[@"买入",@"卖出",@"委托管理",@"交易记录"] withDefaultSelectIndex:1 withType:SliderMenuViewType_Normal];
    [_menuView setTitleLineViewHide:NO];
    _menuView.viewArrays = @[self.buyView,self.sellView,self.proxyView,self.recordView];
    [self addSubview:_menuView];
    
    self.menuView.DidSelectSliderIndex = ^(NSInteger index){
        @StrongObj(self);
//        if (1 == index) {
//            self.coinType = kCoinTypeBTC;
//        } else if (2 == index) {
//            self.coinType = kCoinTypeLite;
//        } else if (3 == index) {
//            self.coinType = kCoinTypeEther;
//        } else if (4 == index) {
//            self.coinType = kCoinTypeEtherClassic;
//        }
    };
}

#pragma mark - public methods
- (void)updateBuyTypeView:(NSString *)type region:(NSString *)r {
    [self.buyView updateTypeView:type region:r];
}

- (void)updateProxyView {
    [self.proxyView updateView];
}

- (void)updateDealView {
    [self.recordView updateView];
}

#pragma mark - TradeBuyViewDelegate
- (void)buyAction {
    if ([self.delegate respondsToSelector:@selector(CNYbuyAction)]) {
        [self.delegate CNYbuyAction];
    }
}

- (void)showTradeTypeAction {
    TradeAlertView *alert = [[TradeAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    alert.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
}

//购买类型
- (void)showTradeTypeStyleAction {
    [self showTradeStyleAlert];
}

#pragma mark - tradeAlertDelegate
- (void)setSelectedAlertListWithModel:(TradeAlertViewModel *)model {
//    self.buyView.regionLabel.text = model.titleStr;
    self.buyView.regionLabel.text = @"USDT";
}
#pragma mark - getter & setter
- (void)setModel:(TradeControlModel *)model {
    self.buyView.model = model;
    self.proxyView.model = model;
    self.recordView.model = model;
}

#pragma mark - tradeSellDelegate
//选择卖出类型
- (void)showTradeSellTypeStyleAction {
    [self showTradeStyleAlert];
}

- (void)showTradeSellTypeAction {
    
}

- (void)sellAction {
    
}

- (void)showTradeStyleAlert {
    LCActionSheet *actionSheet = [LCActionSheet sheetWithTitle:@"委托类型" cancelButtonTitle:@"取消" clicked:^(LCActionSheet *actionSheet, NSInteger buttonIndex) {
        
        NSLog(@"clickedButtonAtIndex: %d, keyWindow: %p", (int)buttonIndex, KEY_WINDOW);
        if (buttonIndex == 1) {
            self.buyView.regionStyle.text = @"限价交易";
            self.sellView.regionStyle.text = @"限价交易";
        }else {
            self.buyView.regionStyle.text = @"市价快速交易";
            self.sellView.regionStyle.text = @"市价快速交易";
        }
    } otherButtonTitles:@"限价交易",@"市价快速交易", nil];
    
    // actionSheet.blurEffectStyle = UIBlurEffectStyleLight;
    
    actionSheet.scrolling          = YES;
    actionSheet.visibleButtonCount = 3.6f;
    
    actionSheet.willPresentHandler = ^(LCActionSheet *actionSheet) {
        NSLog(@"willPresentActionSheet, keyWindow: %p", KEY_WINDOW);
    };
    
    actionSheet.didPresentHandler = ^(LCActionSheet *actionSheet) {
        NSLog(@"didPresentActionSheet, keyWindow: %p", KEY_WINDOW);
    };
    
    actionSheet.willDismissHandler = ^(LCActionSheet *actionSheet, NSInteger buttonIndex) {
        NSLog(@"willDismissWithButtonIndex: %d, keyWindow: %p", (int)buttonIndex, KEY_WINDOW);
    };
    
    actionSheet.didDismissHandler = ^(LCActionSheet *actionSheet, NSInteger buttonIndex) {
        NSLog(@"didDismissWithButtonIndex: %d, keyWindow: %p", (int)buttonIndex, KEY_WINDOW);
    };
    
    [actionSheet show];
}
@end
